#include <crtdbg.h>
#include <stdio.h>
#include <memory.h>

#include "get_downloaders.h"
#include "../connect_tracker/connect_tracker.h"

peer_info_t * get_downloaders(bencode_node_t * node, int * peer_num)
{
	peer_info_t * peer_info_list = NULL, * peer_info_list_head = NULL;
	alp_list_t * peers_list = NULL;
	bencode_node_t * peer_dict = NULL;
	alp_list_index_t * list_index;
	bencode_node_t * peer_info = NULL;
	bencode_node_t * temp_node = NULL;

	(* peer_num) = 0;	

	if (node == NULL || node->type != BENCODE_DICT) return NULL;
	temp_node = (bencode_node_t *) alp_dict_get(node->dict, "peers", 5);

	if (temp_node == NULL || temp_node ->type != BENCODE_LIST) return NULL;
	peers_list = temp_node -> list;
	list_index = alp_list_first(peers_list);
	
	while (list_index != NULL)
	{
		++ (* peer_num);
		list_index = alp_list_next (list_index);
	}

	list_index = alp_list_first(peers_list);
	peer_info_list = (peer_info_t *) malloc ((* peer_num) * sizeof(peer_info_t));
	peer_info_list_head = peer_info_list;

	while (list_index != NULL)
	{
		alp_list_this(list_index, &peer_dict);

		peer_info = (bencode_node_t *) alp_dict_get(peer_dict -> dict, &"peer id", 7);
		peer_info_list -> peer_id = (char *) malloc ((peer_info -> length) + 1);
		memset(peer_info_list -> peer_id , 0, (peer_info -> length) + 1);
		memcpy(peer_info_list -> peer_id, peer_info -> buffer, peer_info -> length);
		peer_info_list -> peer_id_length = peer_info -> length;

		peer_info = (bencode_node_t *) alp_dict_get(peer_dict -> dict, &"ip", 2);
		peer_info_list -> peer_ip = (char *) malloc ((peer_info -> length) + 1);
		memset(peer_info_list -> peer_ip , 0, (peer_info -> length) + 1);
		memcpy(peer_info_list -> peer_ip, peer_info -> buffer, peer_info -> length);
		peer_info_list -> peer_ip_length = peer_info -> length;

		peer_info = (bencode_node_t *) alp_dict_get(peer_dict -> dict, &"port", 4);
		peer_info_list -> peer_port = (char *) malloc ((peer_info -> length) + 1);
		memset(peer_info_list -> peer_port , 0, (peer_info -> length) + 1);
		memcpy(peer_info_list -> peer_port, peer_info -> buffer, peer_info -> length);
		peer_info_list -> peer_port_length = peer_info -> length;

		++ peer_info_list;
		list_index = alp_list_next (list_index);
	}
	return peer_info_list_head;
}

int not_main()
{
	bencode_node_t * node;
	/*FILE * f;
	char* buffer;*/
	size_t len = 100;
/*	int tmp;
*/
	int num;

	peer_info_t * peers;

	char* out;
	int i = connect_tracker("http://59.66.130.44:6969/",25,"2%15%079%c7U3%08%b8%b4%aa%abxi%84%a8%24%b5%40%b3",52,"%00%03BS%e7%26Y%c7%0a%ceG%13z%c5%99%16UDP0",42,"2214",4,"0",1,&out);
	printf("%i\n",i);

	if (out!=NULL) printf("%s\n",out);




//orz boost
/*	tmp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
	tmp |= _CRTDBG_LEAK_CHECK_DF;
	_CrtSetDbgFlag(tmp);

	f = fopen("E:\\2.torrent","rb");
	buffer = (char*) malloc(100+100);
	fread(buffer, 1, 100, f);
	fclose(f);*/
	bencode_parse(&node, out, &i);


	peers = get_downloaders(node, &num);

	for (;num>0;--num)
	{
		printf("%i\n", peers->peer_id_length);
		printf("%s\n", peers->peer_id);
		printf("%s\n", peers->peer_ip);
		printf("%s\n", peers->peer_port);
		printf("%s\n","");
		peers ++;
	}

//	bencode_dump(node,NULL,myprintf);
	alp_bencode_destroy(node);
	
	return 0;
}
